package com.pj.compose.ui.page.widget

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.blankj.utilcode.util.GsonUtils
import com.pj.compose.common.config.StringExt.toast
import com.pj.compose.model.longText
import com.pj.compose.model.simpleItems
import com.pj.compose.ui.compose.*
import com.pj.compose.ui.theme.c3367D5
import com.pj.compose.ui.theme.cEAEDEE

@Composable
fun DialogsPage(navHostController: NavController) {
    val maxContentHeight = (LocalConfiguration.current.screenHeightDp * 0.7).dp
    val primaryColor = MaterialTheme.colorScheme.primary

    /**
     * 菜单列表
     */
    val listItems = listOf(
        "消息类型对话框（蓝色按钮）",
        "消息类型对话框（红色按钮）",
        "消息类型对话框（很长的文案）",
        "菜单类型对话框",
        "带Checkbox的消息确认框",
        "单选菜单类型对话框",
        "多选菜单类型对话框",
        "带输入框的对话框",
        "高度适应键盘升降的对话框",
        "短信验证码对话框",
    )
    //消息弹窗是否显示
    val showDialog = remember {
        mutableStateOf(false)
    }

    //消息弹窗类型
    var messageDialogType by remember {
        mutableStateOf(DialogType.MESSAGE)
    }
    //消息弹窗内容
    var alertMessage by remember {
        mutableStateOf("")
    }
    //消息弹窗内容
    var alertExMessage by remember {
        mutableStateOf("")
    }
    //消息弹窗-确认按钮的文字
    var confirmText by remember {
        mutableStateOf("确认")
    }

    //消息弹窗-确认按钮的颜色
    var confirmColor by remember {
        mutableStateOf(c3367D5)
    }
    //消息弹窗-确认按钮是否显示
    var showConfirm by remember {
        mutableStateOf(true)
    }
    AppTopBarColumn(
        modifier = Modifier
            .fillMaxSize()
            .background(cEAEDEE),
        title = "Dialog", backClick = { navHostController.popBackStack() }
    ) {
        Box {
            Spacer(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(20.dp)
            )
            Divider()
            SimpleListView(listItems) { position ->
                alertMessage = listItems[position]
                alertExMessage = ""
                showConfirm = true
                confirmText = "确认"
                confirmColor = primaryColor
                when (position) {
                    0 -> {
                        messageDialogType = DialogType.MESSAGE
                        confirmColor = c3367D5
                    }
                    1 -> {
                        messageDialogType = DialogType.MESSAGE
                        confirmColor = Color.Red
                        confirmText = "删除"
                    }
                    2 -> {
                        messageDialogType = DialogType.LONG_MESSAGE
                        alertMessage = longText
                        showConfirm = false
                    }
                    3 -> { messageDialogType = DialogType.LIST
                        showDialog.value = true
                    }
                    4 -> {
                        messageDialogType = DialogType.CHECK
                    }
                    5 -> {
                        messageDialogType = DialogType.SINGLE_SELECT
                    }
                    6 -> {
                        messageDialogType = DialogType.MULTI_SELECT
                    }
                    7 -> {
                        messageDialogType = DialogType.INPUT
                    }
                    8 -> {
                        messageDialogType = DialogType.INPUT_EX
                        alertExMessage = longText
                    }
                    9 -> {
                        messageDialogType = DialogType.SMS_CODE
                    }
                }
                if (position != 3)
                    showDialog.value = true
            }
            when (messageDialogType) {
                DialogType.MESSAGE -> {
                    MessageDialog(
                        showDialog = showDialog,
                        title = "消息对话框",
                        content = alertMessage,
                        confirmColor = confirmColor,
                        confirmText = confirmText,
                        showConfirm = showConfirm,
                        onConfirm = {
                            "点击了确认".toast()
                        }
                    )
                }
                DialogType.LONG_MESSAGE -> {
                    LongMessageDialog(
                        showDialog = showDialog,
                        title = "长文本对话框",
                        contentMsg = alertMessage,
                        confirmColor = confirmColor,
                        confirmText = confirmText,
                        maxContentHeight = maxContentHeight,
                    )
                }
                DialogType.CHECK -> {
                    CheckboxDialog(
                        showDialog = showDialog,
                        title = "带checkbox对话框",
                        contentMsg = alertMessage,
                    )
                }
                DialogType.SINGLE_SELECT -> {
                    SingleSelectDialog(
                        showDialog = showDialog,
                        title = "单选对话框",
                        contentItems = simpleItems,
                        confirmColor = confirmColor,
                        confirmText = confirmText,
                        showConfirm = showConfirm,
                        maxContentHeight = maxContentHeight,
                        onConfirm = {
                            "点击了 ${simpleItems[it]}".toast()
                        }
                    )
                }
                DialogType.MULTI_SELECT -> {
                    MultiSelectDialog(
                        showDialog = showDialog,
                        title = "多选对话框",
                        contentItems = simpleItems,
                        confirmColor = confirmColor,
                        confirmText = confirmText,
                        showConfirm = showConfirm,
                        maxContentHeight = maxContentHeight,
                        onConfirm = {
                            "选择了 ${GsonUtils.toJson(it)}".toast()
                        }
                    )
                }
                DialogType.INPUT -> {
                    InputDialog(
                        showDialog = showDialog,
                        title = "带输入对话框",
                        contentMsg = "带输入对话框",
                        confirmColor = confirmColor,
                        confirmText = confirmText,
                        showConfirm = showConfirm,
                        maxContentHeight = maxContentHeight,
                        onConfirm = {
                            "返回 $it".toast()
                        }
                    )
                }
                DialogType.INPUT_EX -> {
                    InputDialog(
                        showDialog = showDialog,
                        title = "带输入对话框",
                        contentMsg = "带输入对话框",
                        extMsg = longText,
                        confirmColor = confirmColor,
                        confirmText = confirmText,
                        showConfirm = showConfirm,
                        maxContentHeight = maxContentHeight,
                        onConfirm = {
                            "返回 $it".toast()
                        }
                    )
                }
                DialogType.LIST -> {
                    ListDialog(
                        showDialog = showDialog,
                        items = simpleItems,
                        modifier = Modifier.sizeIn(maxHeight = maxContentHeight)
                    ) {
                        "点击了选项$it".toast()
                    }
                }
                DialogType.SMS_CODE ->{
                    SmsCodeDialog(showDialog = showDialog, title = "请输入验证码"){
                        "返回了 $it".toast()
                    }
                }
//                else -> {}
            }

        }
    }
}

